//
//  QSSingleSelectionView.swift
//  chat
//
//  Created by 1 on 2023/3/30.
//

import SwiftUI

struct QSSingleSelectionView: View {
    @Binding var selectString:String
    var datas:[String]
    @Binding var isSheetPresented:Bool
    var body: some View {
        VStack(spacing:0){
            Spacer()
            ScrollView(showsIndicators: false) {
                VStack(alignment: .leading,spacing: 0){
                    ForEach(0..<datas.count,id: \.self) { i in
                        let dataString = datas[i]
                        let textColor = (selectString == dataString) ? MBAColor.com_gblue.color : MBAColor.com_white.color
                        HStack(spacing: 0){
                            Text(dataString)
                                .foregroundColor(textColor)
                                .padding(.horizontal,16)
                                .padding(.vertical,14)
                                .font(.system(size: 16))
                                .lineLimit(1)
                                .background(.clear)
                                .frame(minWidth: 0,maxWidth: .infinity,minHeight: 0,maxHeight: .infinity,alignment: .leading)
                            Spacer()
                        }
                        .contentShape(Rectangle())
                        .onTapGesture {
                            selectString = dataString
                            isSheetPresented = false
                        }
                    }
                }
                .padding(.top,4)
                .padding(.bottom,34)
            }
            .frame(height: getScrollH())
            .background(MBAColor.com_textBg.color)
            .cornerRadius(radius: 16,corners: [.topLeft,.topRight])
        }
        .frame(minWidth: 0,maxWidth: .infinity)
    }
    
    func getScrollH()->Double{
        let itemH = 4+(Double)(self.datas.count * 52)+34
        let scrollMaxH = 400.00
        return (itemH>scrollMaxH) ? scrollMaxH : itemH
    }
}

struct QSSingleSelectionView_Previews: PreviewProvider {
//    @State var selectString:String = "a"
    static var previews: some View {
        QSSingleSelectionView(selectString: .constant("a"), datas: ["a","b","c","d"],isSheetPresented: .constant(false))
    }
}
